package com.shopx.config.service.impl;

import com.shopx.base.exception.OriginException;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;
import com.shopx.config.bean.SysConfig;
import com.shopx.config.bean.SysDict;
import com.shopx.config.dao.db1.SysConfigDao;
import com.shopx.config.model.SysConfigModel;
import com.shopx.config.service.ISysConfigService;
import com.shopx.core.common.CommonConstant;
import com.shopx.dal.redis.CacheServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lituo
 * @className: SysConfigServiceImpl
 * @description: TODO
 * @date 2021/12/5 11:24 AM
 */
@Slf4j
@Service
public class SysConfigServiceImpl extends BaseService implements ISysConfigService {
  @Autowired
  private SysConfigDao sysConfigDao;
  @Autowired
  private ISysSeqService sysSeqService;

  @Override
  @Cacheable(value = CommonConstant.CacheKey.CMM, key = "'CONFIG_CONFIGID'+#sysConfig.configId")
  public SysConfigModel selectByPrimaryKey(SysConfig sysConfig) throws ShopxException {
    if (sysConfig == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    return sysConfigDao.selectByPrimaryKey(sysConfig);
  }

  @Override
  public int insertSelective(SysConfig sysConfig) throws ShopxException {
    if (sysConfig == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    String id = sysSeqService.getTabSeq("SysConfig", 4, null);
    sysConfig.setConfigId(id);
    int result = sysConfigDao.insertSelective(sysConfig);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  @CacheEvict(value = CommonConstant.CacheKey.CMM, key = "'CONFIG_CONFIGID'+#sysConfig.configId")
  public int updateByPrimaryKeySelective(SysConfig sysConfig) throws ShopxException {
    if (sysConfig == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysConfigDao.updateByPrimaryKeySelective(sysConfig);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  @CacheEvict(value = CommonConstant.CacheKey.CMM, key = "'CONFIG_CONFIGID'+#sysConfig.configId")
  public int deleteByPrimaryKey(SysConfig sysConfig) throws ShopxException {
    if (sysConfig == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysConfigDao.deleteByPrimaryKey(sysConfig);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int selectCount(SysConfigModel sysConfig) throws ShopxException {
    return sysConfigDao.selectCount(sysConfig);
  }

  @Override
  public List<SysConfigModel> selectList(SysConfigModel sysConfig) throws ShopxException {
    List<SysConfigModel> list = sysConfigDao.selectList(sysConfig);
    return list;
  }

  @Override
  public PageDTO<SysConfigModel> getListPage(SysConfigModel sysConfig) throws ShopxException {
    if (sysConfig == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    PageDTO<SysConfigModel> pageDTO = new PageDTO<SysConfigModel>();
    pageDTO.setiTotalDisplayRecords(selectCount(sysConfig));
    pageDTO.setData(selectList(sysConfig));
    return pageDTO;
  }

  @Override
  public boolean isSuperUser(String userId){
    SysConfig sc = new SysConfig();
    sc.setConfigKey("SUPER_ADMIN");
    SysConfigModel model = this.selectByPrimaryKey(sc);
    if(model==null){
      return false;
    }
    if(model.getConfigValue().equals(userId)){
      return true;
    }
    return false;
  }

  @Override
  public void deleteBatch(List<SysConfig> list) throws ShopxException{
    if(list==null || list.size()==0){
      throw new OriginException("至少删除一条记录");
    }
    sysConfigDao.deleteBatch(list);
  }

}
